package com.ymttest.business.service;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mongodb.BasicDBObject;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productprice.parameter.*;
import com.ymatou.iapi.productquery.parameter.ApplyActivityDto;
import com.ymatou.iapi.selleractivity.service.ApplyActivityCall;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductsInActivity;
import com.ymttest.database.mongo.UserAnalysisMongoWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.mongo.jproduct.CatalogsWapper;
import com.ymttest.database.mongo.jproduct.ProductTimeStampWapper;
import com.ymttest.database.mongo.jproduct.ProductsWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;
import java.util.stream.Collectors;

public class ProductPriceCallService {

	//发布商品 买手信息
	public static int sellerId = Integer.parseInt(EnvSetup
			.getData("productpriceSellerId"));
	public static String sellerName = EnvSetup
			.getData("productpriceSellerName");

	//活动策略模板

	//交易隔离，部分规格，新客
	public static int tpl_isolation_partcatalog_newbuyer = 1;

	//不交易隔离，不部分规格，不新客
	public static int tpl_Nisolation_Npartcatalog_Nnewbuyer = 1;

	//交易隔离，不部分规格，不新客
	public static int tpl_isolation_Npartcatalog_Nnewbuyer = 1;

	//不交易隔离， 部分规格，新客
	public static int tpl_Nisolation_partcatalog_newbuyer = 1;

	///////////////////////////////////////////////////////////////////
	//不交易隔离，不部分规格，不新客，会员
	public static int tpl_Nisolation_Npartcatalog_Nnewbuyer_Member = 7;

	//交易隔离，不部分规格，不新客,会员
	public static int tpl_isolation_Npartcatalog_Nnewbuyer_Member = 7;

	public static void checkPriceListByCatalogs(JSONObject retBean,
			List<ProductPriceBean> tgtProductPriceBean) throws Exception {

	}

	//单商品 多规格
	public static void checkPriceListByCatalogs(JSONObject retBean,
			ProductPriceBean tgtProductPriceBean) throws Exception {
		Logger.verifyEquals(200, retBean.getInt("Code"), "报文返回code检查");
		JSONArray CatalogPriceListArray = retBean.getJSONObject("Data")
				.getJSONArray("CatalogPriceList");
		String productId = tgtProductPriceBean.getProductId();
		//期待规格价格bean
		List<CatalogPriceBean> tgtCatalogPriceBeans = tgtProductPriceBean
				.getCatalogPriceBeans();
		Logger.verifyEquals(tgtCatalogPriceBeans.size(),
				CatalogPriceListArray.length(), "根据规格id查询结果数量比较");

		//组装报文返回值
		ProductPriceBean actProductPriceBean = new ProductPriceBean();
		actProductPriceBean.setProductId(productId);

		List<CatalogPriceBean> catalogPriceBeans = new ArrayList<>();
		for (int i = 0; i < CatalogPriceListArray.length(); i++) {
			JSONObject tempCatalogInfo = CatalogPriceListArray.getJSONObject(i);
			String actProductId = tempCatalogInfo.getString("ProductId");
			JSONObject tempCatalog = tempCatalogInfo
					.getJSONObject("CatalogInfo");

			Logger.verifyEquals(productId, actProductId, "价格服务查询结果productid检查");

			CatalogPriceBean aCatalogPriceBean = new CatalogPriceBean();
			aCatalogPriceBean.setCatalogId(tempCatalog.getString("CatalogId"));
			aCatalogPriceBean.setPrice(tempCatalog.getDouble("Price"));
			aCatalogPriceBean.setActivityId(tempCatalog.getInt("ActivityId"));
			catalogPriceBeans.add(aCatalogPriceBean);
		}
		actProductPriceBean.setCatalogPriceBeans(catalogPriceBeans);

		//实际规格价格bean
		List<CatalogPriceBean> actCatalogPriceBeans = actProductPriceBean
				.getCatalogPriceBeans();

		Logger.verifyEquals(tgtProductPriceBean.getProductId(),
				actProductPriceBean.getProductId(), "商品id 检查");
		Logger.verifyEquals(tgtCatalogPriceBeans.size(),
				actCatalogPriceBeans.size(), "商品catalog id 数量 检查");

		tgtCatalogPriceBeans
				.forEach(tgtBean -> {
					CatalogPriceBean acCatalogPriceBean = actCatalogPriceBeans
							.stream()
							.filter(actBean -> actBean.getCatalogId().equals(
									tgtBean.getCatalogId())).findAny()
							.orElse(null);

					if (acCatalogPriceBean == null) {
						Logger.verifyEquals(false, true, "返回报文中商品id:["
								+ tgtProductPriceBean.getProductId()
								+ "] 缺少catalogId:[" + tgtBean.getCatalogId()
								+ "]");
					} else {
						Logger.verifyEquals(tgtBean.getPrice(),
								acCatalogPriceBean.getPrice(),
								"检查报文返回价格,catalogId:[" + tgtBean.getCatalogId()
										+ "]");
						Logger.verifyEquals(
								tgtBean.getActivityId(),
								acCatalogPriceBean.getActivityId(),
								"检查报文返回活动Id,catalogId:["
										+ tgtBean.getCatalogId() + "]");
					}
				});

	}



	//交易价格服务报文返回值
	private static void checkPriceServiceReturn(JSONObject retBean,
												ProductPriceBean tgtProductPriceBean) throws Exception {

		//组装报文返回值
		JSONArray catalogsJsonArray = retBean.getJSONArray("Catalogs");
		ProductPriceBean actProductPriceBean = new ProductPriceBean();
		actProductPriceBean.setProductId(retBean.getString("ProductId"));

		List<CatalogPriceBean> catalogPriceBeans = new ArrayList<>();
		for (int i = 0; i < catalogsJsonArray.length(); i++) {
			JSONObject catalogInfo = catalogsJsonArray.getJSONObject(i);
			CatalogPriceBean aCatalogPriceBean = new CatalogPriceBean();
			aCatalogPriceBean.setCatalogId(catalogInfo.getString("CatalogId"));
			aCatalogPriceBean.setPrice(catalogInfo.getDouble("Price"));
			aCatalogPriceBean.setActivityId(catalogInfo.getInt("ActivityId"));
			catalogPriceBeans.add(aCatalogPriceBean);
		}
		actProductPriceBean.setCatalogPriceBeans(catalogPriceBeans);

		//期待规格价格bean
		List<CatalogPriceBean> tgtCatalogPriceBeans = tgtProductPriceBean
				.getCatalogPriceBeans();

		//实际规格价格bean
		List<CatalogPriceBean> actCatalogPriceBeans = actProductPriceBean
				.getCatalogPriceBeans();

		Logger.verifyEquals(tgtProductPriceBean.getProductId(),
				actProductPriceBean.getProductId(), "商品id 检查");
		Logger.verifyEquals(tgtCatalogPriceBeans.size(),
				actCatalogPriceBeans.size(), "商品catalog id 数量 检查");

		Logger.verifyEquals(tgtProductPriceBean.getExtraDeliveryFee(),
				retBean.getDouble("ExtraDeliveryFee"), "ExtraDeliveryFee 检查");
		Logger.verifyEquals(tgtProductPriceBean.getExtraDeliveryType(),
				retBean.getInt("ExtraDeliveryType"), "ExtraDeliveryType 检查");

		tgtCatalogPriceBeans
				.forEach(tgtBean -> {
					CatalogPriceBean acCatalogPriceBean = actCatalogPriceBeans
							.stream()
							.filter(actBean -> actBean.getCatalogId().equals(
									tgtBean.getCatalogId())).findAny()
							.orElse(null);

					if (acCatalogPriceBean == null) {
						Logger.verifyEquals(false, true, "返回报文中商品id:["
								+ tgtProductPriceBean.getProductId()
								+ "] 缺少catalogId:[" + tgtBean.getCatalogId()
								+ "]");
					} else {
						Logger.verifyEquals(tgtBean.getPrice(),
								acCatalogPriceBean.getPrice(),
								"检查报文返回价格，catalogId:[" + tgtBean.getCatalogId()
										+ "]");
						Logger.verifyEquals(
								tgtBean.getActivityId(),
								acCatalogPriceBean.getActivityId(),
								"检查报文返回活动Id,catalogId:["
										+ tgtBean.getCatalogId() + "]");
					}
				});

	}

	private static void checkPriceServiceReturnV2(JsonObject retBean,
												ProductPriceBean tgtProductPriceBean) throws Exception {

		//组装报文返回值
		JsonArray catalogsJsonArray = retBean.get("Catalogs").getAsJsonArray();
		ProductPriceBean actProductPriceBean = new ProductPriceBean();
		actProductPriceBean.setProductId(retBean.get("ProductId").getAsString());

		List<CatalogPriceBean> catalogPriceBeans = new ArrayList<>();
		for (int i = 0; i < catalogsJsonArray.size(); i++) {
			JsonObject catalogInfo = catalogsJsonArray.get(i).getAsJsonObject();
			CatalogPriceBean aCatalogPriceBean = new CatalogPriceBean();
			aCatalogPriceBean.setCatalogId(catalogInfo.get("CatalogId").getAsString());
			aCatalogPriceBean.setPrice(catalogInfo.get("Price").getAsDouble());
			aCatalogPriceBean.setActivityId(catalogInfo.get("ActivityId").getAsInt());
			catalogPriceBeans.add(aCatalogPriceBean);
		}
		actProductPriceBean.setCatalogPriceBeans(catalogPriceBeans);

		//期待规格价格bean
		List<CatalogPriceBean> tgtCatalogPriceBeans = tgtProductPriceBean
				.getCatalogPriceBeans();

		//实际规格价格bean
		List<CatalogPriceBean> actCatalogPriceBeans = actProductPriceBean
				.getCatalogPriceBeans();

		Logger.verifyEquals(tgtProductPriceBean.getProductId(),
				actProductPriceBean.getProductId(), "商品id 检查");
		Logger.verifyEquals(tgtCatalogPriceBeans.size(),
				actCatalogPriceBeans.size(), "商品catalog id 数量 检查");

		Logger.verifyEquals(tgtProductPriceBean.getExtraDeliveryFee(),
				retBean.get("ExtraDeliveryFee").getAsDouble(), "ExtraDeliveryFee 检查");
		Logger.verifyEquals(tgtProductPriceBean.getExtraDeliveryType(),
				retBean.get("ExtraDeliveryType").getAsInt(), "ExtraDeliveryType 检查");

		tgtCatalogPriceBeans
				.forEach(tgtBean -> {
					CatalogPriceBean acCatalogPriceBean = actCatalogPriceBeans
							.stream()
							.filter(actBean -> actBean.getCatalogId().equals(
									tgtBean.getCatalogId())).findAny()
							.orElse(null);

					if (acCatalogPriceBean == null) {
						Logger.verifyEquals(false, true, "返回报文中商品id:["
								+ tgtProductPriceBean.getProductId()
								+ "] 缺少catalogId:[" + tgtBean.getCatalogId()
								+ "]");
					} else {
						Logger.verifyEquals(tgtBean.getPrice(),
								acCatalogPriceBean.getPrice(),
								"检查报文返回价格，catalogId:[" + tgtBean.getCatalogId()
										+ "]");
						Logger.verifyEquals(
								tgtBean.getActivityId(),
								acCatalogPriceBean.getActivityId(),
								"检查报文返回活动Id,catalogId:["
										+ tgtBean.getCatalogId() + "]");
					}
				});

	}

	//检查单品价格服务
	public static void checkPriceItem(JSONObject retBean,
									  ProductPriceBean tgtProductPriceBean) throws Exception {
		Logger.verifyEquals(200, retBean.getInt("Code"), "报文返回code检查");
		checkPriceServiceReturn(
				retBean.getJSONObject("Data").getJSONObject("PriceInfo"),
				tgtProductPriceBean);
	}

	public static void checkPriceItemV2(JsonObject retBean,
									  ProductPriceBean tgtProductPriceBean) throws Exception {
		Logger.verifyEquals(200, retBean.get("Code").getAsInt(), "报文返回code检查");
		checkPriceServiceReturnV2(
				retBean.get("Data").getAsJsonObject().get("PriceInfo").getAsJsonObject(),
				tgtProductPriceBean);
	}

	//检查列表价格服务
	public static void checkPriceListV2(JsonObject retBean,
			List<ProductPriceBean> tgtProductPriceBeans) throws Exception {
		Logger.verifyEquals("200", retBean.get("Code"), "报文返回code检查");

		JsonArray productsJsonArray = retBean.get("Data").getAsJsonObject()
				.get("ProductPriceList").getAsJsonArray();
		Logger.verifyEquals(tgtProductPriceBeans.size(),
				productsJsonArray.size(), "价格服务列表查询 商品id 数量查询");

		tgtProductPriceBeans
				.forEach(tgtProductPriceBean -> {
					JsonObject ret = null;
					try {
						for (int i = 0; i < productsJsonArray.size(); i++) {
							JsonObject productBean = productsJsonArray
									.get(i).getAsJsonObject();
							String productIdInRet = productBean
									.get("ProductId").getAsString();
							if (productIdInRet.equals(tgtProductPriceBean
									.getProductId())) {
								ret = productBean;
								break;
							}
						}

						if (ret == null) {
							Logger.verifyEquals(false, true, "返回报文中缺少商品id:["
									+ tgtProductPriceBean.getProductId() + "]");
						} else {
							checkPriceServiceReturnV2(ret, tgtProductPriceBean);
						}

					} catch (Exception e) {
						e.printStackTrace();
					}
				});

	}

	public static void checkPriceList(JSONObject retBean,
									  List<ProductPriceBean> tgtProductPriceBeans) throws Exception {
		Logger.verifyEquals(200, retBean.getInt("Code"), "报文返回code检查");

		JSONArray productsJsonArray = retBean.getJSONObject("Data")
				.getJSONArray("ProductPriceList");
		Logger.verifyEquals(tgtProductPriceBeans.size(),
				productsJsonArray.length(), "价格服务列表查询 商品id 数量查询");

		tgtProductPriceBeans
				.forEach(tgtProductPriceBean -> {
					JSONObject ret = null;
					try {
						for (int i = 0; i < productsJsonArray.length(); i++) {
							JSONObject productBean = productsJsonArray
									.getJSONObject(i);
							String productIdInRet = productBean
									.getString("ProductId");
							if (productIdInRet.equals(tgtProductPriceBean
									.getProductId())) {
								ret = productBean;
								break;
							}
						}

						if (ret == null) {
							Logger.verifyEquals(false, true, "返回报文中缺少商品id:["
									+ tgtProductPriceBean.getProductId() + "]");
						} else {
							checkPriceServiceReturn(ret, tgtProductPriceBean);
						}

					} catch (Exception e) {
						e.printStackTrace();
					}
				});

	}

	//检查搜索结果
	public static void checkPriceForSearchList(JSONObject retBean,
			List<PriceForSearch> priceForSearchList) throws Exception {
		Logger.verifyEquals(200, retBean.getInt("Code"), "报文返回code检查");

		JSONArray productsJsonArray = retBean.getJSONObject("Data")
				.getJSONArray("ProductPriceList");

		priceForSearchList
				.forEach(priceForSearch -> {
					String productId = priceForSearch.getProductId();

					boolean isExistInRet = false;
					for (int i = 0; i < productsJsonArray.length(); i++) {
						try {
							JSONObject ret = productsJsonArray.getJSONObject(i);
							if (productId.equals(ret.getString("ProductId"))) {
								Map<String, Object> tgtBean = MapUtil
										.pojoToMap(priceForSearch);

								Map<String, Object> actBean = MapUtil.hashMap();
								actBean.put("productId", ret.get("ProductId"));
								actBean.put("maxPrice", ret.get("MaxPrice"));
								actBean.put("minPrice", ret.get("MinPrice"));
								actBean.put("minOriginalPrice",
										ret.get("MinOriPrice"));
								actBean.put("maxOriginalPrice",
										ret.get("MaxOriPrice"));
								actBean.put("minNewpersonPrice",
										ret.get("MinNewpPrice"));
								actBean.put("maxNewpersonPrice",
										ret.get("MaxNewpPrice"));
								actBean.put("minVipPrice",
										ret.get("MinVipPrice"));
								actBean.put("maxVipPrice",
										ret.get("MaxVipPrice"));
								actBean.put("priceType", ret.get("PriceType"));
								actBean.put("noOrdersOrAllCancelled",
										ret.get("NoOrdersOrAllCancelled"));
								actBean.put("hasConfirmedOrders",
										ret.get("HasConfirmedOrders"));
								AssertService.assertResultEqual(tgtBean,
										actBean);
								isExistInRet = true;

								return;
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

					Logger.verifyEquals(true, isExistInRet,
							"返回报文中应该包含productid:" + productId);

				});

	}

	//获取码头新客
	public static int getNewCustomer() {
		try {
			/*UserRegisterInfoRequestDtoBean regbean;
			regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			return  urrc.getUserid();*/

			return 3332634;

		} catch (Exception e) {
			e.printStackTrace();
			return 3332634;
        }

	}

	//获取码头老客户
	public static int getOldCustomer() {
		return Integer.parseInt(EnvSetup.getData("oldCustomer"));
	}

	//获取商品所有价格
	public static ProductAllPriceBean getProductAllPrice(String productId,
			int activityId) throws Exception {
		ProductAllPriceBean productAllPriceBean = new ProductAllPriceBean();
		productAllPriceBean.setProductId(productId);

		List<YmtCatalogsI> ymtCatalogsIs = new YmtCatalogsIWapper()
				.selectCatalogsByProductId(productId);

		List<CatalogAllPriceBean> catalogAllPriceBeans = new ArrayList<>();
		ymtCatalogsIs
				.forEach(catalogBean -> {
					CatalogAllPriceBean catalogAllPriceBean = new CatalogAllPriceBean();
					String catalogId = catalogBean.getsCatalogId();

					catalogAllPriceBean.setiNum(catalogBean.getiNum());
					catalogAllPriceBean.setiLimitNum(catalogBean.getiLimitNum());
					catalogAllPriceBean.setCatalogId(catalogId);
					catalogAllPriceBean.setfQuotePrice(catalogBean
							.getfQuotePrice().doubleValue());
					catalogAllPriceBean.setfVipPrice(catalogBean.getfVipPrice()
							.doubleValue());
					catalogAllPriceBean.setfNewCustomerPrice(catalogBean
							.getfNewCustomerPrice().doubleValue());

					//查找活动价格
					YmtProductActivityStockWapper ymtProductActivityStockWapper = new YmtProductActivityStockWapper();
					if (activityId > 0) {
						List<Map> activityStockMap = ymtProductActivityStockWapper
								.selectAllBy(MapUtil.hashMap("scatalogid",
										catalogId));

						//有活动价格
						if (activityStockMap.size() > 0) {
							catalogAllPriceBean.setfActivityPrice(Double
									.parseDouble(activityStockMap.get(0)
											.get("fActivityPrice").toString()));
							catalogAllPriceBean.setActivityId(activityId);
						}
					}
					catalogAllPriceBeans.add(catalogAllPriceBean);

				});

		productAllPriceBean.setCatalogAllPriceBeans(catalogAllPriceBeans);

		return productAllPriceBean;
	}

	public static ProductAllPriceBean getProdPriceWithActivity()
			throws Exception {
		String productId = getProductIdWithAllPrice();
		Logger.comment("商品id: " + productId);
		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		ProductAllPriceBean ret = getProdPriceWithActivity(sellerId, productId,
				catalogs);
		return ret;
	}

	//普通活动，新客活动
	public static ProductAllPriceBean getProdPriceWithActivity_NewBuyerAndNoIsolation()
			throws Exception {
		ProductAllPriceBean productAllPriceBean = getProdPriceWithActivity();

		if (YmtMongoDriverFactroy.IsNewJavaMongoOpen) {
			new ActivityProductsWapper().update(productAllPriceBean
					.getProductId(), MapUtil.hashMap("nbuyer", true,
					"isolation", false, "mlimits", new ArrayList<>()));
		}

		return productAllPriceBean;
	}



	public static ProductAllPriceBean getProdPriceWithActivity_NewBuyerAndNoIsolation(
			int sellerId) throws Exception {
		String productId = getProductIdWithAllPrice(sellerId);
		Logger.comment("商品id: " + productId);
		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

		ProductAllPriceBean productAllPriceBean = getProdPriceWithActivity(
				sellerId, productId,catalogs);
		if (YmtMongoDriverFactroy.IsNewJavaMongoOpen) {
			new ActivityProductsWapper().update(productAllPriceBean
					.getProductId(), MapUtil.hashMap("nbuyer", true,
					"isolation", false, "mlimits", new ArrayList<>()));
		}

		return productAllPriceBean;
	}

	public static List<ProductAllPriceBean> getProdPriceListWithActivity_NewBuyerAndNoIsolation(
			int sellerId, int productCount) throws Exception {
		List<String> prodList = new ArrayList<>();
		for (int i = 0; i < productCount; i++) {
			String productId = getProductIdWithAllPrice(sellerId);
			prodList.add(productId);
			Logger.comment("商品id: " + productId);
		}

		List<ProductAllPriceBean> ret = getProdPriceListWithActivity(sellerId,
				prodList, MapUtil.hashMap("ActivityTemplateId",
						tpl_Nisolation_partcatalog_newbuyer));

		if (YmtMongoDriverFactroy.IsNewJavaMongoOpen) {
			ret.forEach(ele -> {
				ActivityProductsWapper apw = new ActivityProductsWapper();
				try {
					apw.update(ele.getProductId(), MapUtil.hashMap("nbuyer",
							true, "isolation", false, "mlimits",
							new ArrayList<>()));
				} catch (Exception e) {
					e.printStackTrace();
				}
			});

		}
		return ret;
	}

	public static Map getProductAllowBuyInfo() throws Exception {
		String productId = getProductIdWithAllPrice();
		Logger.comment("商品id: " + productId);

		Map m = getProductAllowBuyWithActivity(sellerId, productId,
				MapUtil.hashMap("ActivityTemplateId",
						tpl_Nisolation_partcatalog_newbuyer));
		return m;
	}


	//交易隔离活动，新客活动，
	public static ProductAllPriceBean getProdPriceWithActivity_NewBuyerAndIsolation()
			throws Exception {
		String productId = getProductIdWithAllPrice();
		Logger.comment("商品id: " + productId);

		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		ProductAllPriceBean ret = getProdPriceWithActivity(sellerId, productId,
				catalogs);

		if (YmtMongoDriverFactroy.IsNewJavaMongoOpen) {
			new ActivityProductsWapper().update(ret.getProductId(), MapUtil
					.hashMap("part", true, "nbuyer", true, "isolation", true,
							"mlimits", new ArrayList<>()));
		}

		return ret;
	}

	//普通活动，非新客活动
	public static ProductAllPriceBean getProdPriceWithActivity_NoNewBuyerAndNoIsolation()
			throws Exception {
		ProductAllPriceBean productAllPriceBean = getProdPriceWithActivity();

		if (YmtMongoDriverFactroy.IsNewJavaMongoOpen) {
			new ActivityProductsWapper().update(productAllPriceBean
					.getProductId(), MapUtil.hashMap("part", false, "nbuyer",
					false, "isolation", false, "mlimits", new ArrayList<>()));
		}

		return productAllPriceBean;

	}

	public static ProductAllPriceBean getProdPriceWithActivity_NoNewBuyerAndNoIsolation(
			int sellerId) throws Exception {
//		JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc2.json");
//		JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));
	//	String pic1 = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
	//	String pic2 = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701.jpg";
//		JsonBeanHelper.updateJsonBean(addProductBean.getJSONObject("Product"),MapUtil.hashMap("PictureUrl3",pic1,"PictureUrl2",pic2));
		//String productId = SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");

		AddSellerProductRequest addProductBean = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
		addProductBean.setSellerName(sellerName);
		//addProductBean.getProduct().setProductPicList(MapUtil.hashMap("PictureUrl3",pic1,"PictureUrl2",pic2));
		String productId = ProductManagerCallServiceV3.AddSellerProduct(addProductBean);;
		Logger.comment("商品id: " + productId);

		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		ProductAllPriceBean productAllPriceBean = getProdPriceWithActivity(sellerId, productId,
				catalogs);

		if (YmtMongoDriverFactroy.IsNewJavaMongoOpen) {
			new ActivityProductsWapper().update(productAllPriceBean
					.getProductId(), MapUtil.hashMap(
							"part", false, "nbuyer",
					false, "isolation", false, "mlimits", new ArrayList<>()));
		}

		return productAllPriceBean;
	}


	//交易隔离活动，非新客活动
	public static ProductAllPriceBean getProdPriceWithActivity_NoNewBuyerAndIsolation()
			throws Exception {
		ProductAllPriceBean productAllPriceBean = getProdPriceWithActivity();

		if (YmtMongoDriverFactroy.IsNewJavaMongoOpen) {
			new ActivityProductsWapper().update(productAllPriceBean
					.getProductId(), MapUtil.hashMap("part", false, "nbuyer",
					false, "isolation", true, "mlimits", new ArrayList<>()));
		}

		return productAllPriceBean;
	}

	public static ProductAllPriceBean getProdPriceWithActivity_NoNewBuyerAndIsolation(
			int sellerId) throws Exception {
		/*JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc2.json");
		JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));
		String pic1 = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
		String pic2 = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701.jpg";
		JsonBeanHelper.updateJsonBean(addProductBean.getJSONObject("Product"),MapUtil.hashMap("PictureUrl3",pic1,"PictureUrl2",pic2));*/
		AddSellerProductRequest addProductBean = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
		String productId = ProductManagerCallServiceV3.AddSellerProduct(addProductBean);
		Logger.comment("商品id: " + productId);
		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		ProductAllPriceBean ret = getProdPriceWithActivity(sellerId, productId,
				catalogs);

		if (YmtMongoDriverFactroy.IsNewJavaMongoOpen) {
			new ActivityProductsWapper().update(ret.getProductId(), MapUtil
					.hashMap("part", false, "nbuyer", false, "isolation", true,
							"mlimits", new ArrayList<>()));
		}
		return ret;

	}

	public static String getProductIdWithAllPrice() throws Exception {
//		JSONObject addProductBean = JsonBeanHelper
//				.getJsonBean(ProductManagerCallServiceV2.DefAddSellerProductPath);
//		JsonBeanHelper.updateJsonBean(addProductBean,
//				MapUtil.hashMap("sellerId", sellerId, "sellerName", sellerName));
//		return ProductManagerCallServiceV2.addNewProduct(addProductBean)	.getJSONObject("Data").getString("ProductId");
		AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
		//return ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
		//Add mock wuwen 20210120
		return ProductManagerCallServiceV3.AddSellerProduct(addpro);
	}

	public static String getProductIdWithAllPrice(int sellerId)
			throws Exception {
		/*JSONObject addProductBean = JsonBeanHelper
				.getJsonBean(ProductManagerCallServiceV2.DefAddSellerProductPath);
		JsonBeanHelper.updateJsonBean(addProductBean,
				MapUtil.hashMap("sellerId", sellerId, "sellerName", sellerName));*/
		//return ProductManagerCallServiceV2.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductId");
		AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
		return ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
	}

	//普通活动，非新客活动,会员活动
	public static ProductAllPriceBean getProdPriceWithActivity_NoNewBuyerAndNoIsolation_Member()
			throws Exception {
		String productId = getProductIdWithAllPrice();
		Logger.comment("商品id: " + productId);
		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		ProductAllPriceBean ret = getProdPriceWithActivity(sellerId, productId,
				catalogs);

		if (YmtMongoDriverFactroy.IsNewJavaMongoOpen) {
			new ActivityProductsWapper().update(ret.getProductId(),
					MapUtil.hashMap("part", false, "nbuyer", false,
							"isolation", false));
		}
		return ret;
	}

	//交易隔离活动，非新客活动,会员活动
	public static ProductAllPriceBean getProdPriceWithActivity_NoNewBuyerAndIsolation_Member()
			throws Exception {
		String productId = getProductIdWithAllPrice();
		Logger.comment("商品id: " + productId);
		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		ProductAllPriceBean ret = getProdPriceWithActivity(sellerId, productId,
				catalogs);

		if (YmtMongoDriverFactroy.IsNewJavaMongoOpen) {
			new ActivityProductsWapper()
					.update(ret.getProductId(), MapUtil.hashMap("part", false,
							"nbuyer", false, "isolation", true));
		}
		return ret;
	}

	//发布商品 发布活动 商品参加活动
	public static ProductAllPriceBean getProdPriceWithActivity(int sellerId,
			String productId,List<String> catalogs) throws Exception {

		/*//创建活动
		int activityId = ActivityCallService
				.createActivityInProcess(activityMap);
		Logger.comment("活动id: " + activityId);

		//报名活动
		JSONObject jo = ApplyActivityCall.applyProductInActivity(sellerId, productId,
				activityId, MapUtil.hashMap());
		//失败可能是没找到报名商品 等一下在试试
		if(jo.get("Code").toString().equals("201")){
			Thread.sleep(5000);
			jo = ApplyActivityCall.applyProductInActivity(sellerId, productId,
					activityId, MapUtil.hashMap());
		}
		YmtProductsInActivityWapper ymtProductsInActivityWapper = new YmtProductsInActivityWapper();
		YmtProductsInActivity productsInActivity = ymtProductsInActivityWapper
				.selectByProductId(productId);
		int productInActivityId = productsInActivity.getIproductinactivityid();
		Logger.comment("活动商品id: " + productInActivityId);

		//开始活动二审
		AdminCallService.approveProductInActivity(String
				.valueOf(productInActivityId));

		YMTDateUtil.waitTime(1);*/
		String price1 = "8.0";
		/*Date beginTime1 = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(-3600));
		Date endTime1 = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(3600*4));*/
//		Integer activityId = ApplyHighActivity(price1,productId,beginTime1,endTime1).getActivityId();
		Integer activityId = ProductManagerCallServiceV2.createNoIsolationActivityInProcess();
		List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,price1);
		ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
		return getProductAllPrice(productId, activityId);
	}

	private static List<ProductAllPriceBean> getProdPriceListWithActivity(
			int sellerId, List<String> prodList, Map<String, Object> activityMap)
			throws Exception {
		List<ProductAllPriceBean> productAllPriceBeanList = new ArrayList<>();
		//创建活动
		int activityId = ActivityCallService
				.createActivityInProcess(activityMap);
		Logger.comment("活动id: " + activityId);

		//报名活动
		prodList.forEach(productId -> {
			try {
				ApplyActivityCall.applyProductInActivity(sellerId, productId,
						activityId, MapUtil.hashMap());

				YmtProductsInActivityWapper ymtProductsInActivityWapper = new YmtProductsInActivityWapper();
				YmtProductsInActivity productsInActivity = ymtProductsInActivityWapper
						.selectByProductId(productId);
				int productInActivityId = productsInActivity
						.getIproductinactivityid();
				Logger.comment("活动商品id: " + productInActivityId);

				//开始活动二审
				AdminCallService.approveProductInActivity(String
						.valueOf(productInActivityId));

				YMTDateUtil.waitTime(1);

				productAllPriceBeanList.add(getProductAllPrice(productId,
						activityId));
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		return productAllPriceBeanList;
	}

	/**
	 * 获取ProductAllowBuy info
	 * @param sellerId
	 * @param productId
	 * @param activityMap
	 * @return
	 * @throws Exception
	 */
	public static Map getProductAllowBuyWithActivity(int sellerId,
			String productId, Map<String, Object> activityMap) throws Exception {

		//创建活动
		int activityId = ActivityCallService
				.createActivityInProcess(activityMap);
		Logger.comment("活动id: " + activityId);
		
		//报名活动
		ApplyActivityCall.applyProductInActivity(sellerId, productId,
				activityId, MapUtil.hashMap());

		YmtProductsInActivityWapper ymtProductsInActivityWapper = new YmtProductsInActivityWapper();
		YmtProductsInActivity productsInActivity = ymtProductsInActivityWapper
				.selectByProductId(productId);
		int productInActivityId = productsInActivity.getIproductinactivityid();
		Logger.comment("活动商品id: " + productInActivityId);

		//开始活动二审
		AdminCallService.approveProductInActivity(String
				.valueOf(productInActivityId));

		YMTDateUtil.waitTime(1);
		getProductAllPrice(productId, activityId);
		Map m = new HashMap<>();
		m.put("productId", productId);
		m.put("activityId", activityId);
		m.put("productInActivityId", productInActivityId);
		return m;
	}
	//发布商品 发布活动 商品参加活动
	public static ProductAllPriceBean getProdPriceWithActivityInFuture(
			int sellerId, String productId, Map<String, Object> activityMap)
			throws Exception {

		//创建活动
		int activityId = ActivityCallService
				.createActivityInFuture(activityMap);
		Logger.comment("活动id: " + activityId);

		//报名活动
		ApplyActivityCall.applyProductInActivity(sellerId, productId,
				activityId, MapUtil.hashMap());

		YmtProductsInActivityWapper ymtProductsInActivityWapper = new YmtProductsInActivityWapper();
		YmtProductsInActivity productsInActivity = ymtProductsInActivityWapper
				.selectByProductId(productId);
		int productInActivityId = productsInActivity.getIproductinactivityid();
		Logger.comment("活动商品id: " + productInActivityId);

		//开始活动二审
		AdminCallService.approveProductInActivity(String
				.valueOf(productInActivityId));

		YMTDateUtil.waitTime(1);
		return getProductAllPrice(productId, activityId);
	}

	//获取应显示的价格，忽略活动
	public static ProductPriceBean getProductPriceBeanByUserLevelAndIgnoreActivityPrice(
			ProductAllPriceBean productAllPriceBean, boolean isNewCustomer) throws InterruptedException {
		return getProductPriceBeanByUserLevel(productAllPriceBean,
				isNewCustomer, true);
	}

	//活动商品list
	private static List<String> activityProdList = new ArrayList<>();

	//获取原价或活动价
	public static ProductPriceBean getProductOrigPriceBeanByUserLevel(
			ProductAllPriceBean productAllPriceBean,
			boolean isIgnoreActivityPrice) {
		String productId = productAllPriceBean.getProductId();
		List<CatalogPriceBean> catalogPriceBeans = new ArrayList<>();

		//获取普通价格逻辑
		productAllPriceBean.getCatalogAllPriceBeans().forEach(ele -> {
			CatalogPriceBean catalogPriceBean = new CatalogPriceBean();
			catalogPriceBean.setCatalogId(ele.getCatalogId());
			if (isIgnoreActivityPrice) {

				catalogPriceBean.setPrice(ele.getfQuotePrice());

			} else {
				if (!activityProdList.contains(productId)) {
					YMTDateUtil.waitTime(5);
					activityProdList.add(productId);
				}

				if (ele.getfActivityPrice() > 0) {
					//有活动价 取活动价
				catalogPriceBean.setPrice(ele.getfActivityPrice());
				catalogPriceBean.setActivityId(ele.getActivityId());
			} else {
				//最后取原价
				catalogPriceBean.setPrice(ele.getfQuotePrice());
			}
		}

		catalogPriceBeans.add(catalogPriceBean);
	}	);

		ProductPriceBean productPriceBean = new ProductPriceBean();
		productPriceBean.setProductId(productId);
		productPriceBean.setCatalogPriceBeans(catalogPriceBeans);
		setExtraDeliveryTypeAndExtraDeliveryFee(productPriceBean);
		return productPriceBean;

	}
	/***
	 * 根据用户等级（vip or 新客） 获取普通商品应显示价格
	 * @param isNewCustomer true 为新客， false 为vip
	 * @param isIgnoreActivityPrice 是否忽略活动价格
	 * @return
	 */
	public static ProductPriceBean getProductPriceBeanByUserLevel(
			ProductAllPriceBean productAllPriceBean, boolean isNewCustomer,
			boolean isIgnoreActivityPrice) {
		String productId = productAllPriceBean.getProductId();
		List<CatalogPriceBean> catalogPriceBeans = new ArrayList<>();

		//获取普通价格逻辑
		productAllPriceBean.getCatalogAllPriceBeans().forEach(ele -> {
			CatalogPriceBean catalogPriceBean = new CatalogPriceBean();
			catalogPriceBean.setCatalogId(ele.getCatalogId());
			if (isIgnoreActivityPrice) {
				//忽略活动价格
				if (isNewCustomer && ele.getfNewCustomerPrice() > 0) {
					//新客 同时有新客价 取新客价
					catalogPriceBean.setPrice(ele.getfNewCustomerPrice());
				} else if (ele.getfVipPrice() > 0 && !isNewCustomer) {
					//老客 同时有老客 取老客价
					catalogPriceBean.setPrice(ele.getfVipPrice());
				} else {
					//最后取原价
					catalogPriceBean.setPrice(ele.getfQuotePrice());
				}
			} else {
				if (!activityProdList.contains(productId)) {
					YMTDateUtil.waitTime(5);
					activityProdList.add(productId);
				}

				if (ele.getfActivityPrice() > 0) {
					//有活动价 取活动价
					catalogPriceBean.setPrice(ele.getfActivityPrice());
					catalogPriceBean.setActivityId(ele.getActivityId());
				} else if (isNewCustomer && ele.getfNewCustomerPrice() > 0) {
					//新客 同时有新客价 取新客价
					catalogPriceBean.setPrice(ele.getfNewCustomerPrice());
				} else if (ele.getfVipPrice() > 0) {
					//老客 同时有老客 取老客价
					catalogPriceBean.setPrice(ele.getfVipPrice());
				} else {
					//最后取原价
					catalogPriceBean.setPrice(ele.getfQuotePrice());
				}
			}
			catalogPriceBeans.add(catalogPriceBean);
		});

		ProductPriceBean productPriceBean = new ProductPriceBean();
		productPriceBean.setProductId(productId);
		productPriceBean.setCatalogPriceBeans(catalogPriceBeans);
		setExtraDeliveryTypeAndExtraDeliveryFee(productPriceBean);
		return productPriceBean;

	}

	public static void setExtraDeliveryTypeAndExtraDeliveryFee(
			ProductPriceBean productPriceBean) {
		try {
			Map<String, Object> ret = new ProductsWapper()
					.getByProductId(productPriceBean.getProductId());
			productPriceBean.setExtraDeliveryFee((Double) ret.get("mflight"));
			productPriceBean.setExtraDeliveryType((Integer) ret.get("mdeliv"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//校验非法参数时 返回值
	public static void checkInvalidResponseV2(JsonObject retBean)
			throws Exception {
		Logger.verifyEquals("-2", retBean.get("BCode").getAsString(), "返回报文 BCode字段检查");
		//Logger.verifyEquals("商品信息不存在", retBean.getString("Msg"), "返回报文 Msg字段检查");
		Logger.verifyEquals(true, retBean.get("Data").isJsonNull(),
				"返回报文 Data字段检查");
		//Logger.verifyEquals(true, retBean.get("Trace")==null, "返回报文 Trace字段检查");
	}

	public static void checkInvalidResponse(JSONObject retBean)
			throws Exception {
		Logger.verifyEquals(-2, retBean.getInt("BCode"), "返回报文 BCode字段检查");
		//Logger.verifyEquals("商品信息不存在", retBean.getString("Msg"), "返回报文 Msg字段检查");
		Logger.verifyEquals(true, retBean.isNull("Data"),
				"返回报文 Data字段检查");
		//Logger.verifyEquals(true, retBean.get("Trace")==null, "返回报文 Trace字段检查");
	}

	//修改mongo数据库中已设置的价格 价格+1
	public static ProductAllPriceBean updatePriceInMgo(
			ProductAllPriceBean productAllPriceBean) throws Exception {
		return updatePriceInMgo(productAllPriceBean, 0);
	}


	/***
	 * 价格都加+
	 * @param productAllPriceBean
	 * @param prodInActivityId 活动商品id 如果没有id<0
	 * @return
	 * @throws Exception
	 */
	public static ProductAllPriceBean updatePriceInMgo(
			ProductAllPriceBean productAllPriceBean, int prodInActivityId)
			throws Exception {
		CatalogsWapper catalogsWapper = new CatalogsWapper();
		ProductAllPriceBean newProductAllPriceBean = new ProductAllPriceBean();
		newProductAllPriceBean.setProductId(productAllPriceBean.getProductId());
		List<CatalogAllPriceBean> CatalogAllPriceBeanList = new ArrayList<>();

		//修改普通商品价格
		productAllPriceBean
				.getCatalogAllPriceBeans()
				.forEach(
						catalogAllPriceBean -> {
							String catalogId = catalogAllPriceBean
									.getCatalogId();

							CatalogAllPriceBean newCatalogAllPriceBean = new CatalogAllPriceBean();
							newCatalogAllPriceBean.setCatalogId(catalogId);

							Map<String, Object> updateCatalogPrice = new HashMap<>();

							//原价
							if (catalogAllPriceBean.getfQuotePrice() > 0) {
								updateCatalogPrice
										.put("price", catalogAllPriceBean
												.getfQuotePrice() + 1);
								newCatalogAllPriceBean
										.setfQuotePrice(catalogAllPriceBean
												.getfQuotePrice() + 1);
							}

							//新客价
							if (catalogAllPriceBean.getfNewCustomerPrice() > 0) {
								updateCatalogPrice.put("newp",
										catalogAllPriceBean
												.getfNewCustomerPrice() + 1);
								newCatalogAllPriceBean
										.setfNewCustomerPrice(catalogAllPriceBean
												.getfNewCustomerPrice() + 1);
							}

							//vip价格
							if (catalogAllPriceBean.getfVipPrice() > 0) {
								updateCatalogPrice.put("vip",
										catalogAllPriceBean.getfVipPrice() + 1);
								newCatalogAllPriceBean
										.setfVipPrice(catalogAllPriceBean
												.getfVipPrice() + 1);
							}

							CatalogAllPriceBeanList.add(newCatalogAllPriceBean);

							//update mongo
							try {
								catalogsWapper.update(catalogId,
										updateCatalogPrice);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						});

		//修改活动价格
		if (prodInActivityId > 0) {
			ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
			Map<String, Object> retInMgo = activityProductsWapper
					.getByProdInActivityId(prodInActivityId);

			List<BasicDBObject> updateDBObjects = new ArrayList<>();
			JSONArray catalogsAry = new JSONArray(retInMgo.get("catalogs")
					.toString());
			for (int i = 0; i < catalogsAry.length(); i++) {
				BasicDBObject doc = new BasicDBObject();

				String catalogId = catalogsAry.getJSONObject(i)
						.getString("cid");
				Double price = catalogsAry.getJSONObject(i).getDouble("price");
				int stock = catalogsAry.getJSONObject(i).getInt("stock");

				//如有活动价格 修改活动价格
				CatalogAllPriceBean activityCatalogBean = productAllPriceBean
						.getCatalogAllPriceBeans().stream()
						.filter(ele -> ele.getCatalogId().equals(catalogId))
						.findFirst().orElse(null);
				if (activityCatalogBean != null) {
					price = activityCatalogBean.getfActivityPrice() + 1;
					CatalogAllPriceBeanList
							.stream()
							.filter(ele -> ele.getCatalogId().equals(catalogId))
							.findFirst()
							.orElse(null)
							.setfActivityPrice(
									activityCatalogBean.getfActivityPrice() + 1);
					CatalogAllPriceBeanList
							.stream()
							.filter(ele -> ele.getCatalogId().equals(catalogId))
							.findFirst().orElse(null)
							.setActivityId(activityCatalogBean.getActivityId());
				}

				doc.put("cid", catalogId);
				doc.put("price", price);
				doc.put("stock", stock);
				updateDBObjects.add(doc);
			}

			activityProductsWapper.update(prodInActivityId,
					MapUtil.hashMap("catalogs", updateDBObjects));

		}

		newProductAllPriceBean.setCatalogAllPriceBeans(CatalogAllPriceBeanList);
		return newProductAllPriceBean;
	}

	public static boolean updateActivityStockTo0InMgo(
			ProductAllPriceBean productAllPriceBean, int prodInActivityId)
			throws Exception {
		ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
		Map<String, Object> retInMgo = activityProductsWapper
				.getByProdInActivityId(prodInActivityId);

		List<BasicDBObject> updateDBObjects = new ArrayList<>();
		JSONArray catalogsAry = new JSONArray(retInMgo.get("catalogs")
				.toString());
		for (int i = 0; i < catalogsAry.length(); i++) {
			BasicDBObject doc = new BasicDBObject();

			String catalogId = catalogsAry.getJSONObject(i).getString("cid");
			Double price = catalogsAry.getJSONObject(i).getDouble("price");
			//int stock = catalogsAry.getJSONObject(i).getInt("stock");

			doc.put("cid", catalogId);
			doc.put("price", price);
			doc.put("stock", 0);
			updateDBObjects.add(doc);
		}

		boolean ret = activityProductsWapper.update(prodInActivityId,
				MapUtil.hashMap("catalogs", updateDBObjects));

		new ProductTimeStampWapper().update(productAllPriceBean.getProductId(),
				MapUtil.hashMap("aut", new Date()));

		return ret;
	}



	/***
	 * 
	 * @param productAllPriceBean
	 * @param priceType 0原价，1新客价 2VIP价格，3活动价格
	 * @param hasConfirmedOrders
	 * @return
	 * @throws Exception
	 */

	public static PriceForSearch getPriceForSearch(
			ProductAllPriceBean productAllPriceBean, int priceType,
			boolean hasConfirmedOrders) throws Exception {
		String productId = productAllPriceBean.getProductId();

		ProductsWapper productsWapper = new ProductsWapper();
		Map<String, Object> ret = productsWapper.getByProductId(productId);
		String[] maxp = ret.get("maxp").toString().split(",");
		String[] minp = ret.get("minp").toString().split(",");

		PriceForSearch priceForSearch = new PriceForSearch();
		priceForSearch.setProductId(productAllPriceBean.getProductId());

		priceForSearch.setMaxOriginalPrice(Double.parseDouble(maxp[0]));
		priceForSearch.setMinOriginalPrice(Double.parseDouble(minp[0]));

		priceForSearch.setMaxNewpersonPrice(Double.parseDouble(maxp[1]));
		priceForSearch.setMinNewpersonPrice(Double.parseDouble(minp[1]));

		priceForSearch.setMaxVipPrice(Double.parseDouble(maxp[2]));
		priceForSearch.setMinVipPrice(Double.parseDouble(minp[2]));

		priceForSearch.setHasConfirmedOrders(hasConfirmedOrders);
		priceForSearch.setNoOrdersOrAllCancelled(!hasConfirmedOrders);

		priceForSearch.setPriceType(priceType);
		if (priceType == 0) {
			priceForSearch.setMaxPrice(Double.parseDouble(maxp[0]));
			priceForSearch.setMinPrice(Double.parseDouble(minp[0]));

		} else if (priceType == 1) {
			//有最高新客价格
			if (Double.parseDouble(maxp[1]) > 0) {
				priceForSearch.setMaxPrice(Double.parseDouble(maxp[1]));
				priceForSearch.setMinPrice(Double.parseDouble(minp[1]));

			} else {
				priceForSearch.setMaxPrice(Double.parseDouble(maxp[0]));
				priceForSearch.setMinPrice(Double.parseDouble(minp[0]));
			}

		} else if (priceType == 2) {
			//有最高vip价格
			if (Double.parseDouble(maxp[2]) > 0) {
				priceForSearch.setMaxPrice(Double.parseDouble(maxp[2]));
				priceForSearch.setMinPrice(Double.parseDouble(minp[2]));

			} else {
				priceForSearch.setMaxPrice(Double.parseDouble(maxp[0]));
				priceForSearch.setMinPrice(Double.parseDouble(minp[0]));
			}

		} else if (priceType == 3) {
			if (!activityProdList.contains(productId)) {
				YMTDateUtil.waitTime(5);
				activityProdList.add(productId);
			}

			double max = productAllPriceBean
					.getCatalogAllPriceBeans()
					.stream()
					.max((x, y) -> Double.compare(x.getfActivityPrice(),
							y.getfActivityPrice())).get().getfActivityPrice();

			double min = productAllPriceBean
					.getCatalogAllPriceBeans()
					.stream()
					.min((x, y) -> Double.compare(x.getfActivityPrice(),
							y.getfActivityPrice())).get().getfActivityPrice();

			if (min == 0) {
				min = max;
			}
			priceForSearch.setMaxPrice(max);
			priceForSearch.setMinPrice(min);

		}

		return priceForSearch;

	}

	public static void updateActivityMemberPolicy(int prodInActivityId,
			int type, String value) throws Exception {
		List<BasicDBObject> updateDBObjects = new ArrayList<>();
		BasicDBObject doc = new BasicDBObject();
		doc.put("lt", type);
		doc.put("lv", value);
		updateDBObjects.add(doc);

		new ActivityProductsWapper().update(prodInActivityId,
				MapUtil.hashMap("mlimits", updateDBObjects));

		String productId = new ActivityProductsWapper()
				.getByProdInActivityId(prodInActivityId).get("spid").toString();

		/*new ProductTimeStampWapper().update(productId,
				MapUtil.hashMap("aut", new Date()));*/
		ProductManagerCallServiceV3.updateProductRedisTimeStamp(productId);
	}

	//mongo seller+buyer id
	public static long getUnionId(long buyerId, long sellerId) {
		return (buyerId << 32) + sellerId;
	}

	public static void updateSellerBuyerToVipOrNot(int sellerId, int buyerId,
			boolean vipOrNot) throws Exception {
		long unionId = getUnionId(buyerId, sellerId);

		Date updateDate = vipOrNot
				? YMTDateUtil.getBeforeOrNextDate(-10)
				: YMTDateUtil.getBeforeOrNextDate(-1);
		//new BuyerSellerOrderStatisticsMongoWapper().update(MapUtil.hashMap("_id",unionId), MapUtil.hashMap("FirstPaidTime",updateDate,"NoneOrAllCancelled",false));
		new UserAnalysisMongoWapper().updateFirstPaidTime(unionId, updateDate,
				false);
	}

	/**
	 * 更新mongo活动规格库存
	 * @param prodInActivityId 商品在活动中的id
	 * @throws Exception
	 */
	public static void updateActivityStockTo0InMgo(int prodInActivityId) throws Exception {
		ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
		Map<String, Object> retInMgo = activityProductsWapper
				.getByProdInActivityId(prodInActivityId);

		List<BasicDBObject> updateDBObjects = new ArrayList<>();
		JSONArray catalogsAry = new JSONArray(retInMgo.get("catalogs")
				.toString());
		for (int i = 0; i < catalogsAry.length(); i++) {
			BasicDBObject doc = new BasicDBObject();

			String catalogId = catalogsAry.getJSONObject(i).getString("cid");
			Double price = catalogsAry.getJSONObject(i).getDouble("price");
			//int stock = catalogsAry.getJSONObject(i).getInt("stock");

			doc.put("cid", catalogId);
			doc.put("price", price);
			doc.put("stock", 0);
			updateDBObjects.add(doc);
		}
		boolean ret = activityProductsWapper.update(prodInActivityId,
				MapUtil.hashMap("catalogs", updateDBObjects));
	}

	/**
	 * 获取mongo中的商品规格
	 * @param prodInActivityId
	 * @return
	 * @throws Exception
	 */
	public static JSONArray GetCatalogs(int prodInActivityId) throws Exception {
		ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
		Map<String, Object> retInMgo = activityProductsWapper
				.getByProdInActivityId(prodInActivityId);

		JSONArray catalogsAry = new JSONArray(retInMgo.get("catalogs")
				.toString());
		return catalogsAry;
	}

	/**
	 * 更新商品部分规格为活动库存为0
	 * @param prodInActivityId
	 * @param catalogsAry
	 * @throws Exception
	 */
	public static void updateCatalogStockInMgo(int prodInActivityId,JSONArray catalogsAry) throws Exception {
		List<BasicDBObject> updateDBObjects = new ArrayList<>();
		BasicDBObject doc1 = new BasicDBObject();
		BasicDBObject doc2 = new BasicDBObject();

		String catalogId1 = catalogsAry.getJSONObject(0).getString("cid");
		Double price1 = catalogsAry.getJSONObject(0).getDouble("price");
		//int stock = catalogsAry.getJSONObject(i).getInt("stock");

		doc1.put("cid", catalogId1);
		doc1.put("price", price1);
		doc1.put("stock", 100);

		String catalogId2 = catalogsAry.getJSONObject(1).getString("cid");
		Double price2 = catalogsAry.getJSONObject(1).getDouble("price");

		doc2.put("cid", catalogId2);
		doc2.put("price", price2);
		doc2.put("stock", 0);
		updateDBObjects.add(doc1);
		updateDBObjects.add(doc2);
		ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
		boolean ret = activityProductsWapper.update(prodInActivityId,
				MapUtil.hashMap("catalogs", updateDBObjects));
	}

	/**
	 * 创建一个活动
	 * @param sellerId 买手id
	 * @param productId 商品id
	 * @param activityMap 活动模板
	 * @param beginTime 活动开始时间
	 * @param endTime 活动结束时间
	 * @param alevel 活动的优先级
	 * @param isolation 是否交易隔离
	 * @throws Exception
	 */
	public static Integer getProdPriceWithActivityInFuture(Integer sellerId, String productId, Map<String, Object> activityMap,
														Date beginTime,Date endTime,Integer alevel,boolean isolation)
			throws Exception{
		ProductTimeStampWapper productTimeStampWapper = new ProductTimeStampWapper();
		//创建活动
		int activityId = ActivityCallService
				.createActivityInFuture(activityMap);
		Logger.comment("活动id: " + activityId);
		//报名活动
		ApplyActivityCall.applyProductInActivity(sellerId, productId,
				activityId, MapUtil.hashMap());
		YmtProductsInActivityWapper ymtProductsInActivityWapper = new YmtProductsInActivityWapper();
		YmtProductsInActivity productsInActivity = ymtProductsInActivityWapper
				.selectByProductId(productId);
		int productInActivityId = productsInActivity.getIproductinactivityid();
		Logger.comment("活动商品id: " + productInActivityId);
		//开始活动二审
		AdminCallService.approveProductInActivity(String
				.valueOf(productInActivityId));
		new ProductsWapper().updateActivityProductByProductId(MapUtil.hashMap("aid",activityId),MapUtil.hashMap("isolation",isolation));
		new ProductsWapper().updateActivityProductByProductId(MapUtil.hashMap("aid",activityId),MapUtil.hashMap("alevel",alevel));

		new ProductsWapper().updateActivityProductByProductId(MapUtil.hashMap("aid",activityId),MapUtil.hashMap("start",
				beginTime,"end",endTime));
		productTimeStampWapper.update(productId, MapUtil.hashMap("aut",new Date(),"cut",new Date()));
		return activityId;
	}

	/**
	 * 创建一个高优先级活动
	 * @param price  活动价格
	 * @param productId  商品id
	 * @param beginTime  活动开始时间
	 * @param endTime 活动结束时间
	 * @return
	 * @throws Exception
	 */
	public static ApplyActivityDto ApplyHighActivity(String price,String productId,Date beginTime,Date endTime) throws Exception {
		ProductTimeStampWapper productTimeStampWapper = new ProductTimeStampWapper();
		//获取规格ID
		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		//创建一个高优先级活动  得到活动id
		Integer activityId = ProductManagerCallServiceV2.createNoIsolationActivityInProcess();
		//报名活动  获得商品在活动中的id
		Integer activityStockNum = 100;
		List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, activityStockNum,price);
		//审核通过
		ProductManagerCallServiceV2.approveProductInActivityV2(productId,aids.get(0),true);
		new ProductsWapper().updateActivityProductByProductId(MapUtil.hashMap("aid",activityId),MapUtil.hashMap("start",
				beginTime,"end",endTime));
		productTimeStampWapper.update(productId, MapUtil.hashMap("aut",new Date(),"cut",new Date()));
		ApplyActivityDto applyActivityDto = new ApplyActivityDto();
		applyActivityDto.setActivityId(activityId);
		applyActivityDto.setActivityStockNum(activityStockNum);
		applyActivityDto.setAids(aids);
		applyActivityDto.setCatalogs(catalogs);
		return applyActivityDto;
	}
	
	/**
	 * 创建一个低优先级活动
	 * @param price  活动价格
	 * @param productId  商品id
	 * @param beginTime  活动开始时间
	 * @param endTime 活动结束时间
	 * @return
	 * @throws Exception
	 */
	public static ApplyActivityDto ApplyLowActivity(String price,String productId,Date beginTime,Date endTime) throws Exception {
		ProductTimeStampWapper productTimeStampWapper = new ProductTimeStampWapper();
		ProductsWapper ProductsWapper=new ProductsWapper();
		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		//Integer activityId = ProductManagerCallServiceV2.createNoIsolationActivityInProcessV2();
		Integer activityId = ProductManagerCallServiceV2.createLowActivityInProcess();
		//报名活动  获得商品在活动中的id
		Integer activityStockNum = 100;
		List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, activityStockNum,price);
		//审核通过
		ProductManagerCallServiceV2.approveProductInActivityV2(productId,aids.get(0),true);
		ProductsWapper.updateActivityProductByProductId(MapUtil.hashMap("aid",activityId),MapUtil.hashMap("start",
				beginTime,"end",endTime));
		ProductsWapper.updateActivityProductByProductId(MapUtil.hashMap("aid",activityId),MapUtil.hashMap("isolation",false));
		productTimeStampWapper.update(productId, MapUtil.hashMap("aut",new Date(),"cut",new Date()));
		ApplyActivityDto applyActivityDto = new ApplyActivityDto();
		applyActivityDto.setActivityId(activityId);
		applyActivityDto.setActivityStockNum(activityStockNum);
		applyActivityDto.setAids(aids);
		applyActivityDto.setCatalogs(catalogs);
		return applyActivityDto;
	}
	public static ApplyActivityDto ApplyLowActivity(Integer sellerId,String price,String productId,Date beginTime,Date endTime) throws Exception {
		ProductTimeStampWapper productTimeStampWapper = new ProductTimeStampWapper();
		ProductsWapper ProductsWapper=new ProductsWapper();
		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		Integer activityId = ProductManagerCallServiceV2.createNoIsolationActivityInProcessV2();
		//报名活动  获得商品在活动中的id
		Integer activityStockNum = 100;
		List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, activityStockNum,price);
		//审核通过
		ProductManagerCallServiceV2.approveProductInActivityV2(productId,aids.get(0),true);
		ProductsWapper.updateActivityProductByProductId(MapUtil.hashMap("aid",activityId),MapUtil.hashMap("start",
				beginTime,"end",endTime));
		ProductsWapper.updateActivityProductByProductId(MapUtil.hashMap("aid",activityId),MapUtil.hashMap("isolation",false));
		productTimeStampWapper.update(productId, MapUtil.hashMap("aut",new Date(),"cut",new Date()));
		ApplyActivityDto applyActivityDto = new ApplyActivityDto();
		applyActivityDto.setActivityId(activityId);
		applyActivityDto.setActivityStockNum(activityStockNum);
		applyActivityDto.setAids(aids);
		applyActivityDto.setCatalogs(catalogs);
		return applyActivityDto;
	}
	public static void main(String args[]) throws Exception {
		long a = getUnionId(13542472, 17491511);
		System.out.println(a);
	}

}
